package top.hidoctor.auscultation.feature.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.event.AssessEvent;
import top.hidoctor.auscultation.net.c.ShaichaJieguoContract;
import top.hidoctor.auscultation.net.p.ShaichaJieguoPresenter;
import top.hidoctor.auscultation.net.req.AssessReq;

public class ShaichaJieguoActivity extends BaseActivity implements View.OnClickListener, ShaichaJieguoContract.View {

    private SegmentTabLayout tabYangben;
    private String[] yangben = {"正常", "异常", "待确定"};
    private List<String> diseaseList = new ArrayList<>();
    private CheckBox cbVsd, cbAsd, cbPda, cbPs, cbTof;
    private Button btnSave;
    private ShaichaJieguoPresenter presenter;
    private AssessReq req;
    private EditText editRemark;
    private String id;
    private RelativeLayout rlBingzhong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_shaicha_jieguo);
        presenter = new ShaichaJieguoPresenter(this, this);
        req = new AssessReq();
        id = getIntent().getStringExtra("id");

        initView();
    }

    private void initView() {
        final Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                hideSoft(toolbar);
            }
        });
        tvTitle.setText("主诊医生筛查结果");
        tabYangben = findViewById(R.id.tab_yangben);
        cbVsd = findViewById(R.id.cb_vsd);
        cbAsd = findViewById(R.id.cb_asd);
        cbPda = findViewById(R.id.cb_pda);
        cbPs = findViewById(R.id.cb_ps);
        cbTof = findViewById(R.id.cb_tof);
        btnSave = findViewById(R.id.btn_save);
        editRemark = findViewById(R.id.edit_remark);
        rlBingzhong = findViewById(R.id.rl_bingzhong);
        btnSave.setOnClickListener(this);

        tabYangben.setTabData(yangben);
        tabYangben.setCurrentTab(0);
        req.setConditions("0");
        tabYangben.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                req.setConditions(position + "");
                if (position == 1) {
                    rlBingzhong.setVisibility(View.VISIBLE);
                } else {
                    rlBingzhong.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                if (req.getConditions().equals("1") && !cbVsd.isChecked() && !cbAsd.isChecked() && !cbPda.isChecked() && !cbPs.isChecked() && !cbTof.isChecked()) {
                    showToast("请选择病种");
                    return;
                }
                diseaseList.clear();
                if (cbVsd.isChecked()) diseaseList.add("VSD");
                if (cbAsd.isChecked()) diseaseList.add("ASD");
                if (cbPda.isChecked()) diseaseList.add("PDA");
                if (cbPs.isChecked()) diseaseList.add("PS");
                if (cbTof.isChecked()) diseaseList.add("TOF");
                req.setDisease(diseaseList.toString().trim().substring(1, diseaseList.toString().trim().length() - 1).replace(" ", ""));
                req.setRemake(editRemark.getText().toString().trim());
                presenter.assess(id, req);
                break;
        }
    }

    @Override
    public void assessSuc() {
        finish();
        EventBus.getDefault().post(new AssessEvent());
    }
}
